package com.kuanrf.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 读取config/config.properties
 * <功能详细描述>
 * 
 * @author  wWX166601
 * @version  [版本号, 2013-4-25]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class PropertyUtils
{
    private static final String PATH = "/config/config.properties";
    
    private static Logger logger = Logger.getLogger(PropertyUtils.class);
    
    private static Properties props = null;
    
    private static long start = 0L;
    
    /**
     * <一句话功能简述>
     * <功能详细描述> [参数说明]
     * 
     * @return void [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private static void init()
    {
        long end = System.currentTimeMillis();
        //为空时或加载超过1小时
        if (props == null || end - start >= 3600000)
        {
            start = end;
            
            InputStream in = null;
            props = new Properties();
            try
            {
                in = PropertyUtils.class.getResourceAsStream(PATH);
                props.load(in);
            }
            catch (IOException e)
            {
                logger.error("读取出错配置文件" + PATH + "出错", e);
            }
            finally
            {
                if (in != null)
                {
                    try
                    {
                        in.close();
                    }
                    catch (IOException ex)
                    {
                        logger.error("关闭IO异常", ex);
                    }
                }
            }
        }
    }
    
    /**
     * 获取属性文件值
     * 
     * @param key
     * @return
     */
    public static String getValue(String key)
    {
        init();
        return props.getProperty(key);
    }
    
    public static void main(String[] args)
    {
        long timestamp = System.currentTimeMillis();
        String s = getValue("test");
        System.out.println(s + ": time="
                + (System.currentTimeMillis() - timestamp));
        
    }
}
